package com.lanou3g.code0517;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

@WebServlet(name = "RegisterServlet", urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
    private Controller controller = new Controller();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //TODO 拿到用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String word = request.getParameter("word");
        //TODO 验证
        boolean registerSuccess=
        controller.register(username, password, word);

        String msg = "";
        if (registerSuccess) {
            msg = "Register Success";
        } else {
            msg = "Register Failed";
        }
        //把消息放到request域中发送给jsp
        request.setAttribute("msg", msg);
        request.getRequestDispatcher("/user/msg.jsp").forward(request, response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
